<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\hotel\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use app\hotel\model\HotelCateModel;
use app\hotel\model\HotelConfModel;
use think\Db;
use app\admin\model\ThemeModel;

class HotelCateController extends AdminBaseController
{
    protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];


    /**
     * 房型列表
     */
    public function index()
    {
        $hotelCateModel = new HotelCateModel();
        $categoryTree        = $hotelCateModel->adminCategoryTableTree();

        $this->assign('category_tree', $categoryTree);
        return $this->fetch();
    }
    /**
     * 添加房型列表
     */
    public function add()
    {
        $hotelConfModel = new HotelConfModel();
        $conf = $hotelConfModel->order("id ASC")
            ->where(array('status'=>'1'))
            ->select();
        $this->assign('conf', $conf);

        return $this->fetch();
    }

    /**
     * 添加房型列表逻辑
     */
    public function addPost()
    {
        $hotelCateModel = new HotelCateModel();

        $data = $this->request->param();

        $result = $this->validate($data, 'HotelCate');

        if ($result !== true) {
            $this->error($result);
        }
         $data['conf'] = json_encode($data['conf']);
        if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
            $data['more']['photos'] = [];
            foreach ($data['photo_urls'] as $key => $url) {
                $photoUrl = cmf_asset_relative_url($url);
                array_push($data['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
            }
        }

        $result = $hotelCateModel->addCategory($data);

        if ($result === false) {
            $this->error('添加失败!');
        }

        $this->success('添加成功!', url('HotelCate/index'));

    }

    /**
     * 编辑房型
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $category = HotelCateModel::get($id)->toArray();

            $hotelCateModel = new HotelCateModel();
            $category['conf'] = json_decode($category['conf']);

            if(!empty($category['conf'])){

               $category['conf']  =implode(',',$category['conf']);
            }

            $this->assign($category);

            $hotelConfModel = new HotelConfModel();
            $conf = $hotelConfModel->order("id ASC")
                ->where(array('status'=>'1'))
                ->select();
            $this->assign('confs', $conf);

            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }

    }

    /**
     * 编辑房型提交

     */
    public function editPost()
    {
        $data = $this->request->param();


        $data['conf'] = json_encode($data['conf']);
        $hotelCateModel = new HotelCateModel();

        if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
            $data['more']['photos'] = [];
            foreach ($data['photo_urls'] as $key => $url) {
                $photoUrl = cmf_asset_relative_url($url);
                array_push($data['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
            }
        }
        $result = $hotelCateModel->editCategory($data);

        if ($result === false) {
            $this->error('保存失败!');
        }

        $this->success('保存成功!');
    }


    /**
     * 房型排序

     */
    public function listOrder()
    {
        parent::listOrders(Db::name('portal_category'));
        $this->success("排序更新成功！", '');
    }

    /**
     * 删除房型

     */
    public function delete()
    {
        $hotelCateModel = new HotelCateModel();
        $id                  = $this->request->param('id');
        //获取删除的内容
        $findCategory = $hotelCateModel->where('id', $id)->find();

        if (empty($findCategory)) {
            $this->error('房型不存在!');
        }




        $data   = [
            'object_id'   => $findCategory['id'],
            'create_time' => time(),
            'table_name'  => 'portal_category',
            'name'        => $findCategory['name']
        ];
        $result = $hotelCateModel
            ->where('id', $id)
            ->update(['delete_time' => time()]);
        if ($result) {
            Db::name('recycleBin')->insert($data);
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
   /**
    *  发布房型
    * */
    public function publish()
    {
        $param           = $this->request->param();
        $hotelCateModel = new HotelCateModel();

        if (isset($param['id']) && isset($param["yes"])) {
            $id = $this->request->param('id');

            $hotelCateModel->where(['id' =>  $id])->update(['status' => 1, 'published_time' => time()]);

            $this->success("发布成功！");
        }

        if (isset($param['id']) && isset($param["no"])) {
            $id = $this->request->param('id');

            $hotelCateModel->where(['id' => ['in', $id]])->update(['status' => 0]);

            $this->success("取消发布成功！");
        }

    }
}
